"""
Case Type   : Operator
Case Name   : opengauss关键字operator(非保留)，自定义操作符，操作符类，操作符族 合理报错
Create At   : 2025.3.24
Owner       : wang-tianjing1
Description :
    1、为数据类型box定义了一种新的操作符
    2、为数据类型_int4(int4数组) 定义了一个 GiST 索引操作符类
    3、定义操作符族
Expect      :
    1、失败
    2、失败
    3、成功
History     : by wang-tianjing1 2025/3/27 与开发确认CREATE OPERATOR FAMILY可以成功，删除需要打开
              support_extended_features参数
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class OpreatorTest(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('dbuser')
        self.user_node = Node('dbuser')
        self.constant = Constant()

    def test_dolphin(self):
        text = '----step1:下面的命令为数据类型box定义了一种新的操作符--面积相等；expect：失败----'
        self.log.info(text)
        sql_cmd = f'CREATE OPERATOR === ('\
                  f'LEFTARG = box,'\
                  f'RIGHTARG = box,'\
                  f'PROCEDURE = area_equal_procedure,'\
                  f'COMMUTATOR = ===,' \
                  f'NEGATOR = !==,' \
                  f'RESTRICT = area_restriction_procedure,' \
                  f'JOIN = area_join_procedure,' \
                  f'HASHES, MERGES);'
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn("function area_equal_procedure(box, box) does not exist",
                      sql_res, '执行失败' + text)

        text = '----step2:下面的例子命令为数据类型_int4(int4数组) 定义了一个 GiST 索引操作符类;expect:失败----'
        self.log.info(text)
        sql_cmd1 = f'CREATE OPERATOR CLASS gist__int_ops\n'\
                   f'DEFAULT FOR TYPE _int4 USING gist AS\n'\
                   f'OPERATOR        3       &&,'\
                   f'OPERATOR        6       = (anyarray, anyarray),'\
                   f'OPERATOR        7       @>,' \
                   f'OPERATOR        8       <@,' \
                   f'OPERATOR        20      @@ (_int4, query_int),' \
                   f'FUNCTION        1       g_int_consistent (internal, _int4, int, oid, internal),' \
                   f'FUNCTION        2       g_int_union (internal, internal),' \
                   f'FUNCTION        3       g_int_compress (internal),' \
                   f'FUNCTION        4       g_int_decompress (internal),' \
                   f'FUNCTION        5       g_int_penalty (internal, internal, internal),' \
                   f'FUNCTION        6       g_int_picksplit (internal, internal),' \
                   f'FUNCTION        7       g_int_same (_int4, _int4, internal);'
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1)
        self.log.info(sql_res1)
        self.assertIn("ERROR:  operator does not exist", sql_res1, '执行失败' + text)

        text = '----step3:定义操作符族;expect:成功----'
        self.log.info(text)
        text = '-----设置support_extended_features参数为on，数据库重启成功------'
        self.log.info(text)
        result = self.sh_primary.execute_gsguc('reload',
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           "support_extended_features = on")
        self.log.info(result)
        msg = self.sh_primary.restart_db_cluster()
        self.log.info(msg)
        sql_cmd2 = f'DROP OPERATOR FAMILY IF EXISTS name_001 USING btree;'\
                   f'CREATE OPERATOR FAMILY name_001 USING btree;'\
                   f'DROP OPERATOR FAMILY name_001 USING btree;'
        sql_res2 = self.sh_primary.execut_db_sql(sql_cmd2)
        self.log.info(sql_res2)
        self.assertIn("CREATE OPERATOR FAMILY", sql_res2, '执行失败' + text)
        self.assertIn("DROP OPERATOR FAMILY", sql_res2, '执行失败' + text)
        text = '-----恢复support_extended_features参数，数据库重启成功------'
        self.log.info(text)
        result = self.sh_primary.execute_gsguc('reload',
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           "support_extended_features = off")
        self.log.info(result)
        msg = self.sh_primary.restart_db_cluster()
        self.log.info(msg)

    def tearDown(self):
        self.log.info('-----------无需清理环境----------')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
